﻿#include"Tree.h"


//前序遍历
void preOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	printf("%c ", root->data);
	preOrder(root->left);
	preOrder(root->right);
}

//中序遍历---左根右
void inOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL");
		return;
	}
	inOrder(root->left);
	printf(" %c ", root->data);
	inOrder(root->right);
}

//后序遍历---左右根
void postOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL");
		return;
	}
	postOrder(root->left);
	postOrder(root->right);
	printf(" %c ", root->data);
}

//二叉树节点个数
//int BinaryTreesize(BTNode* root)
//{
//	if (root == NULL)
//	{
//		return 0;
//	}
//	//结点非空+1
//	static int size = 0;
//	size++;
//	BinaryTreesize(root->left);
//	BinaryTreesize(root->right);
//	return size;
//}
//int BinaryTreesize(BTNode* root, int* psize)
//{
//	if (root == NULL)
//	{
//		return 0;
//	}
//	//结点非空+1
//	(*psize)++;
//	BinaryTreesize(root->left,psize);
//	BinaryTreesize(root->right,psize);
//	return *psize;
//}
//二叉树节点个数
int BinaryTreesize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	return 1 + BinaryTreesize(root->left) + BinaryTreesize(root->right);
}

// ⼆叉树叶⼦结点个数
int BinaryTreeLeafSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	if (root->left == NULL && root->right == NULL)
	{
		return 1;
	}
	return  BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}

// ⼆叉树第k层结点个数
int BinaryTreeLevelKSize(BTNode* root, int k)
{
	if (root == NULL)
	{
		return 0;
	}
	if (k == 1)
	{
		return 1;
	}
	return BinaryTreeLevelKSize(root->left, k - 1) + BinaryTreeLevelKSize(root->right, k - 1);
}

//⼆叉树的深度/⾼度
int BinaryTreeDepth(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	int leftDep = BinaryTreeDepth(root->left);
	int rightDep = BinaryTreeDepth(root->right);
	return 1 + (leftDep > rightDep ? leftDep : rightDep);
}

// ⼆叉树查找值为x的结点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
	if (root == NULL)
	{
		return NULL;
	}
	if (root->data == x)
	{
		return root;
	}
	 BTNode*leftNode = BinaryTreeFind(root->left, x);
	 if (leftNode)
	 {
		 return leftNode;
	 }
	 BTNode* rightNode = BinaryTreeFind(root->right, x);
	 if (rightNode)
	 {
		 return rightNode;
	 }
	 return NULL;
}